//
//  ServicesDetails.m
//  Truffle
//
//  Created by Ismaila Kredity on 08/02/10.
//  Copyright 2010 kredity. All rights reserved.
//

#import "ServiceDetailsViewController.h"
#import "Comments.h"
#import "RestConnection.h"
#import "DatasTruffle.h"
#import "ServiceListViewController.h"
#import "Service.h"
#import "Gateway.h"

@implementation ServiceDetailsViewController

@synthesize titre, position, auteur, description, chmpTxtDescription, idServ, assignedService, myPass, gatewayIndexSection, gatewayIndexRow;


 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

#pragma mark RestConnectionDelegate

- (void)willSendRequest:(NSURLRequest *)request
{
}

- (void)didReceiveResponse:(NSURLResponse *)response
{
	NSLog(@">>[ServiceDetails -- didReceiveResponse] %@", response);
}

- (void)finishedReceivingData:(NSData *)data callback:(NSString *)restconn
{
	
	NSLog(@">>[ServiceDetails --  finishedReceivingData] %@", [restConnection stringData]);
	
	// SET STATIC SERVICE DETAILS
	//NSDictionary *rawDetailsData = [NSDictionary dictionaryWithJSONString:@"{\"name\":\"StaticTestService\",\"position\":\"45-46\",\"informations\":[{\"label\":\"ouverture\",\"value\":\"Lundi au Vendredi 9h00-19h00\",\"type\":\"DESC\"},{\"label\":\"telephone\",\"value\":\"01 44 27 23 01\",\"type\":\"DESC\"},{\"label\":\"email\",\"value\":\"bibmie@upmc.fr\",\"type\":\"DESC\"},{\"label\":\"places disponibles\",\"value\":\"325/500 places\",\"type\":\"QTE\"}]}"];
	
	// Get the dictionary containing the raw parsed data. (DYNAMIC)
	NSDictionary *rawDetailsData = [restConnection dictionaryData];
	 
	// Get the interesting search results data for display to the user.
	[tableData release];
	tableData = [[rawDetailsData objectForKey:@"informations"] retain];
	
	// SET VALUES
	//[self.titre setText:[rawDetailsData objectForKey:@"name"]];
	[self.titre setText:[NSString stringWithFormat:@"%@",assignedService.servicename]];
	[self.position setText:[NSString stringWithFormat:@"%@",assignedService.position]];
	[self.auteur setText:[NSString stringWithFormat:@"%@",assignedService.provider]];
	
	if ([rawDetailsData objectForKey:@"name"] == NULL) {
		[self.description setText:@"Les détails du service ne sont pas disponibles."];
	} else {
		for (int cpt=0; cpt<[tableData count]; cpt++){
			// Extract info from JSon string
			[self.description setText:[self.description.text stringByAppendingFormat:@"%@ : %@\n",[[tableData objectAtIndex:cpt] objectForKey:@"label"], [[tableData objectAtIndex:cpt] objectForKey:@"value"]]];
		}
	}
	
	[activityIndicator stopAnimating];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
    [super viewDidLoad];
	
	[activityIndicator setHidesWhenStopped:TRUE];
	[activityIndicator startAnimating];
	
	
	
	self.title = @"Détails";
	self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];

	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
											  initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
											  target:self action:@selector(comments:)];

	restConnection = [RestConnection new];
	restConnection.baseURLString = [NSString stringWithFormat:@"%@/iphone/service/%@", [[[self.myPass gatewayList] objectAtIndex:gatewayIndexSection] ip], [assignedService serviceid]];
	restConnection.delegate = self;

	
	NSString *urlString = @"";
	[restConnection performRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
	
	NSLog(@">>[ServiceDetails] Request launched : %@",restConnection.baseURLString);

}


- (void) comments:(id)sender {
	
	if(comment == nil)
		comment = [[Comments alloc] initWithNibName:@"Comments" bundle:nil];
	comment.indexPass = self.gatewayIndexSection;
	comment.assignedService = self.assignedService;
	[self.navigationController pushViewController:comment animated:YES];
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
